﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class PropertyListControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }

        protected void gvProperty_Edit(object sender, CommandEventArgs e)
        {
            int propid = Convert.ToInt32(e.CommandArgument.ToString());
            if (propid > 0)
                Response.Redirect("RSPropertyDetail.aspx?propid=" + propid);
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if(userIdentity.RSCanWrite)
                Response.Redirect("~/RS/PRM/RSPropertyDetail.aspx?action=add");
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        public bool LoadPropertyList(int companyid)
        {
            bool bHasValue = true;

            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            String sql = "";
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if(userIdentity.RSGroupName == "DIRECTOR")
                sql = "exec rs_uspGetPropertiesByCompanyID @CompanyId";
            else
                sql = "exec rs_uspGetPropertiesByManagerID @CompanyID, @ManagerID";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sql, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@ManagerID";
            param.Value = userIdentity.RSEmployeeId;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            //Session["DATATABLE"] = ds.Tables[0];
            gvPropertyList.DataSource = ds;
            gvPropertyList.DataBind();
            myConnection.Close();

            if (gvPropertyList.Rows.Count == 0)
            {
                bHasValue = false;
                return bHasValue;
            }
            return bHasValue;
        }
        
        //private string ConvertSortDirectionToSql(SortDirection sortDirection)
        //{
        //    string newSortDirection = String.Empty;

        //    switch (sortDirection)
        //    {
        //        case SortDirection.Ascending:
        //            newSortDirection = "ASC";
        //            break;

        //        case SortDirection.Descending:
        //            newSortDirection = "DESC";
        //            break;
        //    }
        //    return newSortDirection;
        //}

        protected void gvPropertyList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPropertyList.PageIndex = e.NewPageIndex;
            gvPropertyList.DataBind();
        }

        //protected void gvPropertyList_Sorting(object sender, GridViewSortEventArgs e)
        //{
        //    DataTable dataTable = Session["DATATABLE"] as DataTable;
        //    if (dataTable != null)
        //    {
        //        DataView dataView = new DataView(dataTable);
        //        dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        //        gvPropertyList.DataSource = dataView;
        //        gvPropertyList.DataBind();
        //    }
        //}

    }
}